/**
 * Writed by yura and mak
 */

package wz.legend.handlers;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import wz.legend.Context;
import wz.legend.HttpContext;
import wz.legend.Status;
import wz.legend.util.Utils;


/**
 * 
 * @author yura
 * 
 */
public class SleepHandler implements ResponseHandler {
    private static final Logger LOG = Logger.getLogger(SleepHandler.class);

    private Status workStatus;
    private Status sleepStatus = Status.INACTIVE;
    private Status lastStatus;

    private int minWorkTime;
    private int maxWorkTime;

    private int minSleepTime;
    private int maxSleepTime;

    private Timer statusTimer = new Timer();
    private TimerTask workTask;
    private TimerTask sleepTask;

    public void handle(HttpContext httpContext, Context context) {
        if (lastStatus != context.getStatus()) {
            if (lastStatus == null) {
                lastStatus = context.getStatus();
            }

            if (context.getStatus() == workStatus) {
                createTasks(context);
            } else {
                if (workTask != null)
                    workTask.cancel();
                if (sleepTask != null)
                    sleepTask.cancel();
            }
        }

        lastStatus = context.getStatus();
    }

    private void createTasks(Context context) {
        workTask = new DelayChangerTask(context, sleepStatus);
        sleepTask = new DelayChangerTask(context, workStatus);
        int workTime = Utils.getRandomInterval(minWorkTime, maxWorkTime);
        int sleepTime = Utils.getRandomInterval(minSleepTime, maxSleepTime);
        if (workTime > 0 && sleepTime > 0) {
            statusTimer.schedule(workTask, workTime);
            statusTimer.schedule(sleepTask, workTime + sleepTime);
        }
    }

    public class DelayChangerTask extends TimerTask {
        private Context context;
        private Status status;

        public DelayChangerTask(Context context, Status status) {
            this.context = context;
            this.status = status;
        }

        @Override
        public void run() {
            context.setStatus(status);
            lastStatus = status;
            if (status == workStatus) {
                createTasks(context);
            }
            LOG.debug("status changed " + status.name());
        }
    }

    public Status getWorkStatus() {
        return workStatus;
    }

    public void setWorkStatus(Status workStatus) {
        this.workStatus = workStatus;
    }

    public Status getSleepStatus() {
        return sleepStatus;
    }

    public void setSleepStatus(Status sleepStatus) {
        this.sleepStatus = sleepStatus;
    }

    public int getMinWorkTime() {
        return minWorkTime;
    }

    public void setMinWorkTime(int minWorkTime) {
        this.minWorkTime = minWorkTime;
    }

    public int getMaxWorkTime() {
        return maxWorkTime;
    }

    public void setMaxWorkTime(int maxWorkTime) {
        this.maxWorkTime = maxWorkTime;
    }

    public int getMinSleepTime() {
        return minSleepTime;
    }

    public void setMinSleepTime(int minSleepTime) {
        this.minSleepTime = minSleepTime;
    }

    public int getMaxSleepTime() {
        return maxSleepTime;
    }

    public void setMaxSleepTime(int maxSleepTime) {
        this.maxSleepTime = maxSleepTime;
    }
}
